package org.bonorsoft.modules.user.controller;

import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.config.Global;
import org.bonorsoft.common.constant.CacheConstant;
import org.bonorsoft.common.system.api.ISysBaseAPI;
import org.bonorsoft.common.system.vo.LoginUser;
import org.bonorsoft.common.util.PasswordUtil;
import org.bonorsoft.common.util.StringUtils;
import org.bonorsoft.common.util.file.FileUploadUtils;
import org.bonorsoft.modules.system.entity.SysUser;
import org.bonorsoft.modules.system.service.ISysDictService;
import org.bonorsoft.modules.system.service.ISysUserService;
import org.bonorsoft.modules.sysutils.login.LoginInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 个人信息 业务处理
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/user/profile")
public class SysProfileController
{
    private static final Logger log = LoggerFactory.getLogger(SysProfileController.class);

    private String prefix = "system/user/profile";

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDictService dictService;


    /**
     * 个人信息
     */
    @GetMapping()
    public String profile(ModelMap mmap)
    {
        LoginUser user = LoginInfo.getLoginUser();
        mmap.put("user", user);
        if(user.getDeptId() != null){
            mmap.put("deptName", dictService.queryTableDictTextByKey("sys_dept","dept_name","dept_id",user.getDeptId().toString()));
        }
        return prefix + "/profile";
    }

    @GetMapping("/checkPassword")
    @ResponseBody
    public boolean checkPassword(String password)
    {
        return LoginInfo.matches(password);

    }

    @GetMapping("/resetPwd")
    public String resetPwd(ModelMap mmap)
    {
        LoginUser user = LoginInfo.getLoginUser();
        mmap.put("user", userService.getById(user.getId()));
        return prefix + "/resetPwd";
    }

    @AutoLog(value = "重置密码", operateType = 3)
    @PostMapping("/resetPwd")
    @ResponseBody
    public Result<?> resetPwd(String oldPassword, String newPassword)
    {
        LoginUser user = LoginInfo.getLoginUser();
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(user, sysUser);
        if (StringUtils.isNotEmpty(newPassword) && LoginInfo.matches(oldPassword))
        {
            sysUser.setPassword(newPassword);
            return userService.changePassword(sysUser);
        }else {
            return Result.error("修改密码失败，旧密码错误");
        }
    }

    /**
     * 修改头像
     */
    @GetMapping("/avatar")
    public String avatar(ModelMap mmap)
    {
        LoginUser user = LoginInfo.getLoginUser();
        mmap.put("user", user);
        return prefix + "/avatar";
    }

    /**
     * 修改用户
     */
    @AutoLog(value = "个人信息-修改用户", operateType = 3)
    @PostMapping("/update")
    @ResponseBody
    public Result<?> update(SysUser user)
    {
        userService.updateByUser(user);
        return Result.ok("修改用户成功！");
    }

    /**
     * 保存头像
     */
    @AutoLog(value = "个人信息-保存头像", operateType = 3)
    @PostMapping("/updateAvatar")
    @ResponseBody
    public Result<?> updateAvatar(@RequestParam("avatarfile") MultipartFile file)
    {
        SysUser sysUser = new SysUser();
        LoginUser currentUser = LoginInfo.getLoginUser();
        BeanUtils.copyProperties(currentUser, sysUser);
        try
        {
            if (!file.isEmpty())
            {
                String avatar = FileUploadUtils.upload(Global.getAvatarPath(), file);
                sysUser.setAvatar(avatar);
                if (userService.updateByUser(sysUser))
                {
                    return Result.ok();
                }
            }
            return Result.error("修改头像失败!");
        }
        catch (Exception e)
        {
            log.error("修改头像失败！", e);
            return Result.error(e.getMessage());
        }
    }
}
